package com.eduagent.xwqeduagent.app.RoutingStudentAgent;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.map.MapUtil;
import com.eduagent.xwqeduagent.app.RoutingStudentAgent.enums.AgentTypeEnum;
import com.eduagent.xwqeduagent.app.RoutingStudentAgent.prompt.consultAgentSystemPrompt;
import com.eduagent.xwqeduagent.constant.Constant;
import com.eduagent.xwqeduagent.openFeign.client.AdminClient;
import com.eduagent.xwqeduagent.openFeign.entity.UserAndRole;
import com.eduagent.xwqeduagent.openFeign.result.Result;
import com.eduagent.xwqeduagent.tools.studenttools.CourseTools;
import lombok.RequiredArgsConstructor;
import org.springframework.ai.chat.client.advisor.QuestionAnswerAdvisor;
import org.springframework.ai.chat.client.advisor.api.Advisor;
import org.springframework.ai.vectorstore.SearchRequest;
import org.springframework.ai.vectorstore.VectorStore;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * 课程咨询智能体
 */
@Component
@RequiredArgsConstructor
public class ConsultAgent extends AbstractAgent {

    private final AdminClient adminClient;
    private final consultAgentSystemPrompt systemPromptConfig;
    private final VectorStore vectorStore;
    private final CourseTools courseTools;

    @Override
    public String systemMessage() {
        return consultAgentSystemPrompt.SYSTEM_PROMPT;
    }

    @Override
    public AgentTypeEnum getAgentType() {
        return AgentTypeEnum.CONSULT;
    }

    @Override
    public List<Advisor> advisors() {
        var searchRequest = SearchRequest.builder().query("").topK(999).build();
        return List.of(new QuestionAnswerAdvisor(vectorStore, searchRequest));
    }

    @Override
    public Object[] tools() {
        return new Object[]{courseTools};
    }

    @Override
    public Map<String, Object> toolContext(String sessionId, String requestId) {
        //  获取当前用户ID
        //获取当前登录用户
        Result<UserAndRole> result = adminClient.getById();
        if (result == null || result.getData() == null) {
            throw new RuntimeException("获取用户信息失败");
        }
        UserAndRole userAndRole = result.getData();
        String userId = userAndRole.getId();
        return MapUtil.<String, Object>builder() // 设置tool列表
                .put(Constant.USER_ID, userId) // 设置用户id参数
                .put(Constant.REQUEST_ID, requestId) // 设置请求id参数
                .build();
    }

    @Override
    public Map<String, Object> systemMessageParams() {
        return Map.of("now", DateUtil.now());
    }
}
